<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_tasks.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_customfields.php");
require_once ("./modules/teamwork/functions_teamwork.php");
require_once ("./modules/teamwork/functions_tasks.php");
require_once ("./modules/teamwork/functions_taskrender.php");
$template->loadLanguageSection("staff_twtasks");

if ($eventaction != "edittask")
{
	$interface->assignQuickLink($_SWIFT["language"]["simplelist"], "index.php?_m=teamwork&_a=tasks&viewtype=".TASKVIEW_SIMPLE, "icon_taskview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["detailedlist"], "index.php?_m=teamwork&_a=tasks&viewtype=".TASKVIEW_DETAILED, "icon_taskview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["nextsevendays"], "index.php?_m=teamwork&_a=tasks&viewtype=".TASKVIEW_NEXTSEVENDAYS, "icon_taskview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["overduetasks"], "index.php?_m=teamwork&_a=tasks&viewtype=".TASKVIEW_OVERDUE, "icon_taskview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["byassignment"], "index.php?_m=teamwork&_a=tasks&viewtype=".TASKVIEW_BYASSIGNMENT, "icon_taskview.gif");
}

/**
* ###############################################
* TASKS
* ###############################################
*/
if ($eventaction == "tasks")
{
	unset($extendsql);

	if (empty($_REQUEST["viewtype"]))
	{
		$_viewtype = TASKVIEW_SIMPLE;
	} else {
		$_viewtype = $_REQUEST["viewtype"];
	}

	if ($_viewtype == TASKVIEW_NEXTSEVENDAYS)
	{
		$sevendaystimeline = DATENOW+(7*86400);
		$extendsql = " AND (`duedate` > '". DATENOW ."' AND `duedate` < '". intval($sevendaystimeline) ."') AND `iscomplete` = '0'";
	} else if ($_viewtype == TASKVIEW_OVERDUE) {
		$extendsql = " AND (`duedate` < '". DATENOW ."' AND `duedate` != '0') AND `iscomplete` = '0'";
	} else if ($_viewtype == TASKVIEW_COMPLETED) {
		$extendsql = " AND `iscomplete` = '1'";
	} else {
		$extendsql = " AND `iscomplete` = '0'";
	}

	if (!empty($_GET["inserttask"]))
	{
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["taskinsertconfirm"], htmlspecialchars($_GET["inserttask"]));
	} else if (!empty($_GET["updatetask"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["taskupdateconfirm"], htmlspecialchars($_GET["updatetask"]));
	} else if (!empty($_GET["deltask"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["taskdelconfirm"], htmlspecialchars($_GET["deltask"]));
	}

	$options = getTaskGridOptions($_viewtype);

	$options["recordsperpage"] = "10";

	$_gridsort = getTaskGridSort($_viewtype);
	$options["sortby"] = $_gridsort[0];
	$options["appendurl"] = "&viewtype=".$_viewtype;
	$options["hiddenfields"]["viewtype"] = $_viewtype;

	$options["sortorder"] = $_gridsort[1];
	$options["massaction"][0]["title"] = $_SWIFT["language"]["markascomplete"];
	$options["massaction"][0]["callback"] = "_maMarkAsComplete";
	$options["massaction"][1]["title"] = $_SWIFT["language"]["markasincomplete"];
	$options["massaction"][1]["callback"] = "_maMarkAsInComplete";
	$options["massaction"][2]["title"] = $_SWIFT["language"]["markasoverdue"];
	$options["massaction"][2]["callback"] = "_maMarkAsOverdue";
	$options["massaction"][3]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][3]["callback"] = "_maDeleteTasks";

	$options["idname"] = "calendartaskid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["subject"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'calendartasks` WHERE ((`tasktype` = \''. SWIFTPUBLIC .'\') OR (`tasktype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') OR (`ownerstaffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) AND `subject` LIKE \'%$_searchstr%\''.$extendsql.' $_sortjoin;';

	$options["callback"] = "_taskFields";

	$fields = getTaskGridFields($_viewtype);

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["tasks"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=tasks" title="'.$_SWIFT["language"]["tasks"].'">'.$_SWIFT["language"]["tasks"].'</a>', "", 7);

	$grid->start("ct".$_viewtype, $_SWIFT["language"]["tasks"], 'SELECT * FROM `'. TABLE_PREFIX .'calendartasks` WHERE ((`tasktype` = \''. SWIFTPUBLIC .'\') OR (`tasktype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') OR (`ownerstaffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\'))'.$extendsql.' $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'calendartasks` WHERE ((`tasktype` = \''. SWIFTPUBLIC .'\') OR (`tasktype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') OR (`ownerstaffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\'))'.$extendsql.';', 'SELECT * FROM `'. TABLE_PREFIX .'calendartasks` WHERE ((`tasktype` = \''. SWIFTPUBLIC .'\') OR (`tasktype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') OR (`ownerstaffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) AND `subject` LIKE \'%$_searchstr%\''.$extendsql.' $_sortjoin;', $fields, $options);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	$grid->display("ct".$_viewtype);

	$interface->staffFooter();






/**
* ###############################################
* INSERT TASK
* ###############################################
*/
} else if ($eventaction == "inserttask") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "")
		{
			$errormessage = $_SWIFT["language"]["subjectempty"];
		} else if (empty($_POST["startdate"]) || !checkCustomFieldContent(CUSTOMFIELD_TASK)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$_startdate = calenderDateToDateline($_POST["startdate"]);
			if (empty($_POST["duedate"]))
			{
				$_duedate = $_startdate+86400;
			} else {
				$_duedate = calenderDateToDateline($_POST["duedate"]);
			}
			if ($_duedate < $_startdate)
			{
				$_duedate = $_startdate+86400;
			}

			if ($_POST["tasktype"] == SWIFTPRIVATE)
			{
				$_POST["ownerstaffid"] = $_SWIFT["staff"]["staffid"];
			}

			$_POST["completepercent"] = intval(str_replace("%","", $_POST["completepercent"]));

			if ($_POST["completepercent"] == "100")
			{
				$iscomplete = "1";
			} else {
				$iscomplete = "0";
			}

			if (intval($_POST["completepercent"]) > 100)
			{
				$_POST["completepercent"] = 100;
			}

			$calendartaskid = insertCalendarTask($_POST["subject"], $_POST["tasktype"], $_POST["ownerstaffid"], $_duedate, $_startdate, 0, $iscomplete, $_POST["completepercent"], $_POST["calendarlabelid"], $_POST["calendarstatusid"], $_POST["calendarpriorityid"], $_POST["body"]);

			if ($calendartaskid)
			{
				updateCustomFields(CUSTOMFIELD_TASK, $calendartaskid);
			}

			printRedirect(sprintf($_SWIFT["language"]["taskinsertconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=teamwork&_a=tasks&inserttask=".urlencode($_POST["subject"]));
			exit;
		}
	}

	$template->assign("calendar", true);

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["inserttask"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=tasks" title="'.$_SWIFT["language"]["tasks"].'">'.$_SWIFT["language"]["tasks"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=inserttask" title="'.$_SWIFT["language"]["inserttask"].'">'.$_SWIFT["language"]["inserttask"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTaskForm(INSERT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=tasks");

	$interface->staffFooter();




/**
* ###############################################
* EDIT TASK
* ###############################################
*/
} else if ($eventaction == "edittask") {
	$_task = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."calendartasks` AS calendartasks LEFT JOIN `". TABLE_PREFIX ."calendardata` AS calendardata ON (calendardata.datatype = '". TEAMWORK_TASK ."' AND calendardata.typeid = calendartasks.calendartaskid) WHERE calendartasks.calendartaskid = '". intval($_REQUEST["calendartaskid"]) ."';");
	if (empty($_task["calendartaskid"]) || ($_task["taskttype"] == SWIFTPRIVATE && $_task["staffid"] != $_SWIFT["staff"]["staffid"] && $_task["ownerstaffid"] != $_SWIFT["staff"]["staffid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtask"], E_USER_ERROR);
	}

	if ($_GET["mode"] == "delete")
	{
		deleteCalendarTasks(array($_task["calendartaskid"]));

		printRedirect(sprintf($_SWIFT["language"]["taskdelconfirm"], htmlspecialchars($_task["subject"])), "index.php?_m=teamwork&_a=tasks&deltask=".urlencode($_task["subject"]));
		exit;
	} else if ($_GET["mode"] == "complete") {
		markTasksAsComplete(array($_task["calendartaskid"]));
		$_task["iscomplete"] = "1";
		$_task["completepercent"] = "100";
		$infomessage = sprintf($_SWIFT["language"]["taskcompstatus"], htmlspecialchars($_task["subject"]));
	} else if ($_GET["mode"] == "incomplete") {
		markTasksAsInComplete(array($_task["calendartaskid"]));
		$_task["iscomplete"] = "0";
		$infomessage = sprintf($_SWIFT["language"]["taskcompstatus2"], htmlspecialchars($_task["subject"]));
	}
	

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "")
		{
			$errormessage = $_SWIFT["language"]["subjectempty"];
		} else if (empty($_POST["startdate"]) || !checkCustomFieldContent(CUSTOMFIELD_TASK)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$_startdate = calenderDateToDateline($_POST["startdate"]);
			if (empty($_POST["duedate"]))
			{
				$_duedate = $_startdate+86400;
			} else {
				$_duedate = calenderDateToDateline($_POST["duedate"]);
			}
			if ($_duedate < $_startdate)
			{
				$_duedate = $_startdate+86400;
			}

			if ($_POST["tasktype"] == SWIFTPRIVATE)
			{
				$_POST["ownerstaffid"] = $_SWIFT["staff"]["staffid"];
			}

			$_POST["completepercent"] = intval(str_replace("%","", $_POST["completepercent"]));

			if ($_POST["completepercent"] == "100")
			{
				$iscomplete = "1";
			} else {
				$iscomplete = "0";
			}

			if (intval($_POST["completepercent"]) > 100)
			{
				$_POST["completepercent"] = 100;
			}

			updateCalendarTask($_task["calendartaskid"], $_POST["subject"], $_POST["tasktype"], $_POST["ownerstaffid"], $_duedate, $_startdate, 0, $iscomplete, $_POST["completepercent"], $_POST["calendarlabelid"], $_POST["calendarstatusid"], $_POST["calendarpriorityid"], $_POST["body"]);

			updateCustomFields(CUSTOMFIELD_TASK, $_task["calendartaskid"]);

			printRedirect(sprintf($_SWIFT["language"]["taskupdateconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=teamwork&_a=tasks&updatetask=".urlencode($_POST["subject"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_task);

	$template->assign("calendar", true);

	$template->assign("itemoptiontitle", $_SWIFT["language"]["taskoptions"]);

	if ($_task["iscomplete"] == "0")
	{
		$interface->assignItemOption($_SWIFT["language"]["markascomplete"], "", "icon_check.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=edittask&calendartaskid='.intval($_task["calendartaskid"]).'&mode=complete\');');
	} else {
		$interface->assignItemOption($_SWIFT["language"]["markasincomplete"], "", "icon_deletesmall.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=edittask&calendartaskid='.intval($_task["calendartaskid"]).'&mode=incomplete\');');
	}
	$interface->assignItemOption($_SWIFT["language"]["deletetask"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=edittask&calendartaskid='.intval($_task["calendartaskid"]).'&mode=delete\');');

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["edittask"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=tasks" title="'.$_SWIFT["language"]["tasks"].'">'.$_SWIFT["language"]["tasks"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=edittask&calendartaskid='. intval($_task["calendartaskid"]) .'" title="'.$_SWIFT["language"]["edittask"].'">'.$_SWIFT["language"]["edittask"].' ('. htmlspecialchars($_task["subject"]) .')</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTaskForm(EDIT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=tasks");

	$interface->staffFooter();
}


?>